package com.atguigu.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 告诉spring这个是一个切面类
 */
@Aspect
public class LogAspects {
    //抽取公共切入点
    //1、本类引用-直接方法名
    //2、其它的切面引用-全类名
    @Pointcut("execution(public int com.atguigu.aop.MathCalculator.*(..))")
    public void pointCut(){};
    //在目标方法之前切入-切入表达式
//    @Before("public int com.atguigu.aop.MathCalculator.div(int, int)")
//    @Before("public int com.atguigu.aop.MathCalculator.*(..)")
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("方法名:"+joinPoint.getSignature().getName()+"...除法运行...参数列表是:{"+ Arrays.asList(args) +"}");
    }

    //
//    @After("public int com.atguigu.aop.MathCalculator.*(..)")
    @After("com.atguigu.aop.LogAspects.pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println("方法名:"+joinPoint.getSignature().getName()+"...除法结束。。。");
    }

    @AfterThrowing(pointcut = "pointCut()", throwing = "expect")
    public void logThrow(Exception expect) {
        System.out.println("除法出现异常...错误信息:{"+expect+"}");
    }

    @AfterReturning(pointcut = "pointCut()", returning = "retVal")
    public void logReturn(JoinPoint joinPoint, Object retVal) {
        System.out.println("方法名:"+joinPoint.getSignature().getName()+"除法正常结束...运行返回值:{"+retVal+"}");
    }

    
}
